/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Vue;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;

import App.Controleur.Jeu;

public class Panneau_Jeu extends JPanel_Principal{

	private List<String[]> bateaux;
	private Panneau_Plateau plateau;
	private Vue_Bateau bateauJoueur;
	private Vue_Bateau bateauCible;
	private Vue_Repaire repaireCible;
	private Panneau_Reparer reparer;
	private Panneau_Actions actions;
	private Panneau_Informations infos;
	private static Panneau_Jeu panneauJeu;
	private Image table;
	private boolean flagInit = true;
	protected Jeu controleur;


	/**
	 * Créer un pannel de jeu
	 * @param bateaux Liste contenant la couleur du bateau ainsi que sa position
	 * tableau de String[3]{"Couleur","position X", "position Y"}
	 * @param premierJoueur couleur du premier joueur ex "rouge"
	 * @param pieces taleau String[6] contenant les pieces ex {"pirate","canon",coffre5",""...}
	 * @param scores Contient les infos par joueurs, tableau de 3 strings :[0]nom [1]nbTresor au Port [2]nbTresor bateau
	 */
	public Panneau_Jeu(final Jeu controleur, int nbJoueur){
		panneauJeu = this;
		this.controleur = controleur;
		JButton options = new JButton("Options");
		options.setFont(boutonFont);
		options.setForeground(Color.BLACK);
		options.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				new Panneau_Option(panneauJeu, controleur);
			}
		});
		this.setLayout(new GridBagLayout());
		
		initialiser(nbJoueur);
		//Création des panels

		//Panneau_Popup test = new Panneau_Popup(this);

		//Ajout des éléments au panel
		this.addItem(infos			, 0, 0, 1, 1, GridBagConstraints.WEST,1,GridBagConstraints.NONE);
		this.addItem(plateau		, 1, 0, 1, 1, GridBagConstraints.CENTER,0,GridBagConstraints.NONE);
		this.addItem(options		, 2, 0, 1, 1, GridBagConstraints.NORTHEAST,1,GridBagConstraints.NONE);	
		this.addItem(bateauJoueur	, 0, 1, 1, 1, GridBagConstraints.WEST,0,GridBagConstraints.NONE);
		this.addItem(bateauCible	, 2, 1, 1, 1, GridBagConstraints.EAST,0,GridBagConstraints.NONE);
		this.addItem(repaireCible	, 2, 1, 1, 1, GridBagConstraints.EAST,0,GridBagConstraints.NONE);
		this.addItem(reparer		, 1, 1, 1, 1, GridBagConstraints.CENTER,1,GridBagConstraints.BOTH);
		this.addItem(actions		, 1, 1, 1, 1, GridBagConstraints.WEST,1,GridBagConstraints.VERTICAL);

	}

	private void initialiser(int nbJoueur){
		
		bateaux = new ArrayList<String[]>();
		List<String[]> scoreInit = new ArrayList<String[]>();
		for(int i=0;i<nbJoueur;i++){
			bateaux.add(new String[] {""+i,"0","0"});
		}
		plateau = new Panneau_Plateau(bateaux,controleur);		
		String[] emplacements = {"0","0","0","0","0","0"};
		bateauJoueur = new Vue_Bateau("0", emplacements,"0");
		bateauCible = new Vue_Bateau("0", emplacements,"0");
		repaireCible = new Vue_Repaire(1, emplacements, 2);
		reparer = new Panneau_Reparer(this.controleur,this,emplacements, 6);
		actions = new Panneau_Actions(controleur,0, 0, 0, 0, false, false,false);
		infos = new Panneau_Informations(scoreInit);

		repaireCible.setVisible(false);
		actions.setVisible(false);
	}

	/**
	 * Ajoute un élément à la grille
	 * @param c compasant
	 * @param x colonne
	 * @param y ligne
	 * @param width largeur du composant
	 * @param height hauteur du composant
	 * @param align ancre d'alignement
	 * @param weiY poid hauteur
	 * @param fill consigne de remplissage
	 */
	private void addItem(JComponent c, int x, int y, int width, int height, int align,double weiY,int fill) {
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = x;
		gc.gridy = y;
		gc.gridwidth = width;
		gc.gridheight = height;
		gc.weightx = 1;
		gc.weighty = weiY;
		gc.insets = new Insets(5, 5, 5, 5);
		gc.anchor = align;
		gc.fill = fill;
		this.add(c, gc);
	}

	/**
	 * Peint l'arrière plan
	 */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		if(flagInit){
			Rectangle screenBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
			try {
				BufferedImage bufferedFond = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("img/backgroundTable.jpg"));
				table = bufferedFond.getScaledInstance(screenBounds.width,screenBounds.height, 0);

			} catch (IOException e) {
				e.printStackTrace();
			}
			flagInit = false;
		}
		g.drawImage(table, 0, 0, this);
	}

	//Met à jour le panneau de jeu
	public void updateReparer(String[] pieces,int credits){
		System.out.println("UPDATE REPARER ------------------------");
		actions.setVisible(false);
		reparer.setVisible(true);
		reparer.updateReparer(pieces, credits);
	}
	
	public void updatePlateau(List<String[]> bateaux){
		plateau.updatePlateau(bateaux);
	}

	public void updateActions(int nbTresorPort, int nbTresorBateau, int nbCanons, int nbPirates, boolean attaquable,boolean deplacable, boolean reparable){

		reparer.setVisible(false);
		actions.setVisible(true);
		actions.updateActions(nbTresorPort, nbTresorBateau, nbCanons, nbPirates, false, false, false);
	}

	public void updateBateauJoueur(String couleur, String[] pieces, String nbTresors){
		bateauJoueur.updateBateau(couleur, pieces,nbTresors);
	}

	public void updateBateauCible(String couleur, String[] pieces, String nbTresors){
		repaireCible.setVisible(false);
		bateauCible.setVisible(true);
		bateauCible.updateBateau(couleur, pieces,nbTresors);
	}

	public void updateRepaireCible(int repaire,String[] emplacements,int nbTresors){
		bateauCible.setVisible(false);
		repaireCible.setVisible(true);
		repaireCible.updateRepaire(repaire, emplacements, nbTresors);
	}

	public void updateInformations(List<String[]> scores){
		infos.updateScores(scores);
	}
	//Actions sur le panneau réparer
	public void retourClicReparer(){
		reparer.setVisible(false);
		actions.setVisible(true);
		updateReparer(true);
	}
	
	public void validerClicReparer(){
		updateReparer(false);
		reparer.setVisible(false);
		actions.setVisible(true);
	}
	
	//Update du panneau action
	public void updateAttaquer(boolean attaquable, String position){
		actions.updateAttaquer(attaquable, position);
	}
	
	public void updateDeplacer(boolean deplacable, String position){
		actions.updateDeplacer(deplacable, position);
	}
	
	public void updateReparer(boolean reparable){
		actions.updateReparer(reparable);
	}
}
